$(function () {
  Statistics = 1;
  initStatisticsData();
});

function initStatisticsData() {
  const username = localStorage.getItem("user");
  const currYear = localStorage.getItem("year");
  const currMon = localStorage.getItem("month");
  const currDept = localStorage.getItem("dept");
  $('.statistics-bg .title').text(`${currYear}年预算执行情况统计`)
  //请求参数
  var obj0 = {
    username: username,
    budgetYear: currYear,
    budgetMonth: currMon,
    dept: currDept == 'null' ? '' : currDept,
    status: 2,
    chartPage: 70501,
    chartNo: 1
  }
  //API
  ApiGet("getdata", obj0, function (e) {
    var json = JSON.parse(e);
	if(json.items[0].charts[0].data.length>0){
		let data = json.items[0].charts[0].data[0].key01;
		$(".str").text(data)
	}
    
  });


  //请求参数
  var obj = {
    username: username,
    budgetYear: currYear,
    budgetMonth: currMon,
    dept: currDept == 'null' ? '' : currDept,
    status: 2,
    chartPage: 2,
    chartNo: 1
  }
  // API
  ApiGet("getdata", obj, function (e) {
    var json = JSON.parse(e);
    let data = json.items[0].charts[0].data;
    data = data.slice(1)
    let treeData = []
    data.forEach((item) => {
      let obj = {
        id: item.key01,
        name: item.key03,
        fatherId: item.key02,
      }
      treeData.push(obj);
    })
    // console.log(treeData, '菜单表')
    // 处理数据
    //1、 先把数据处理成map
    const map = {};
    const val = [];
    treeData.forEach((item) => {
      map[item.id] = item;
    });
    //2、 遍历之后，形成树形结构
    treeData.forEach((item) => {
      const parent = map[item.fatherId];
      if (parent) {
        (parent.children || (parent.children = [])).push(item);
      } else {
        val.push(item)
      }
    });
    // console.log("映射", map)
    // console.log("新数组", val)
    let treeMenu1 = ''
    let treeMenu2 = ''
    let treeMenu3 = ''

    // treeMenu(val)
    for (let i = 0; i < val.length; i++) {
      if (val[i].children) {
        treeMenu1 += `
        <div class="MenuItem">
        <p>${val[i].name}</p>
        <img src="./assets/images/statistics/enter.png" alt=" ">
      </div>`

        treeMenu2 = ''
        for (let o = 0; o < val[0].children.length; o++) {
          treeMenu1 += `
          <div class="MenuItem">
          <p>${val[0].children[o].name}</p>
          <img src="./assets/images/statistics/enter.png" alt=" ">
        </div>`

          treeMenu3 = ''
          if (val[0].children[0].children) {
            for (let p = 0; p < val[0].children[0].children.length; p++) {
              // console.log(val[i].children[o])
              treeMenu1 += `
              <div class="MenuItem">
              <p>${val[0].children[0].children[p].name}</p>
              <img src="./assets/images/statistics/enter.png" alt=" ">
            </div>`

            }
          }
          // console.log(treeMenu1)
        }
      }

    }
    $(".treeMenu1").html(treeMenu1)
    $(".treeMenu2").html(treeMenu2)
    $(".treeMenu3").html(treeMenu3)
  });

  //来款预算总额
  var obj = {
    username: getUsername,
    budgetYear: fullYear,
    budgetMonth: fullMonth,
    dept: fullDepartment,
    status: 2,
    chartPage: 70404,
    chartNo: 1
  }
  //API
  ApiGet("getdata", obj, function (e) {
    var json = JSON.parse(e);
    let data = json.items[0].charts[0].data;

    let wancheng = `<div style="display: flex; justify-content: space-around;margin-bottom: 5px;">
    <div class="progress-bar">
    <div style="width: ${data[0].key03 >= 100 ? 100 : data[0].key03}%; background-color:${data[0].key03 >= 100 ? "#F53F3F" : "#165DFF"}" class="progress"></div>
  </div>
    
    &nbsp;
    <span>${data[0].key03}%</span>
  </div>
  <div class="yusuan">
    <p> 预算总额 <span>${data[0].key01} <span class="dw">万元</span> </span></p>
    <p> 实际来款 <span>${data[0].key02} <span class="dw">万元</span> </span></p>
    <!-- 支出执行 -->
  </div>`
    $(".charts1").html(wancheng)
  });

  //支出预算总额
  var obj = {
    username: getUsername,
    budgetYear: fullYear,
    budgetMonth: fullMonth,
    dept: fullDepartment,
    status: 2,
    // chartPage: 70404,
    // chartNo: 5
	chartPage: 7040401,
	chartNo: 3,
	budgetNo: '0001'
  }
  //API
  ApiGet("getdata", obj, function (e) {
    var json = JSON.parse(e);
    let data = json.items[0].charts[0].data;
    // console.log(data[0], '支出预算总额')
    let wancheng = `<div style="display: flex; justify-content: space-around;margin-bottom: 5px;">
      <div class="progress-bar">
      <div style="width: ${data[0].key04 >= 100 ? 100 : data[0].key04}%; background-color:${data[0].key04 >= 100 ? "#F53F3F" : "#165DFF"}" class="progress"></div>
    </div>
      &nbsp;
      <span>${data[0].key04}%</span>
    </div>
    <div class="yusuan">
      <p> 预算总额 <span>${data[0].key01} <span class="dw">万元</span> </span></p>
      <p> 支出执行 <span>${data[0].key02} <span class="dw">万元</span> </span></p>
      
    </div>`
    $(".charts2").html(wancheng)
  });
}

$('.statistics-bg .detail').click(function () {
  $('.mask-wrap .mask-write-area textarea').val($('.detail .str').text())
  $('#editMask').show()
})
$('.mask-wrap .btn button.save').click(function () {
  ApiGet('getdata', {
    username: localStorage.getItem("user"),
    budgetYear: localStorage.getItem("year"),
    budgetMonth: localStorage.getItem("month"),
	dept: fullDepartment,
    status: 2,
    chartPage: 70501,
    chartNo: 2,
    key01: $('.mask-wrap .mask-write-area textarea').val()
  }, function (res) {
    let re = eval("(" + res + ")");
    $('#editMask').hide()
    alert(re.items[0].charts[0].chart_name)
    initStatisticsData();
  });


})
$(".cancle").click(function () {
  $('#editMask').hide()
})
$(".mask-wrap .mask-title img").click(function () {
  $('#editMask').hide()
})